﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Deflection
{
    class Path
    {
        public int CurrentNodeIndex { get; set; }

        /// <summary>
        /// The Nodes on the path to travel to
        /// </summary>
        public List<Vector2> Nodes { get; set; }

        /// <summary>
        /// The center of the path on the screen
        /// </summary>
        public Vector2 position = Vector2.Zero;

        /// <summary>
        /// The scale of the path, bigger = Nodes are more spread out
        /// </summary>
        public float scale = 1.0f;

        public Path()
        {
            CurrentNodeIndex = 0;
            Nodes = new List<Vector2>();
        }

        // Could add a reverse path feature

        /// <summary>
        /// Checks if the path has all the right components set, ready to be used
        /// </summary>
        /// <returns>True if valid</returns>
        public bool CheckPathIsValid()
        {
            if(Nodes.Count < 1) return false;
            if(position == Vector2.Zero) return false;
            if(scale <= 0.0f) return false;

            // All is good
            return true;          
        }

        /// <summary>
        /// Gets the Vector2 position of the current path node
        /// </summary>
        /// <returns></returns>
        public Vector2 GetCurrentPositionOnPath()
        {
            return Nodes[CurrentNodeIndex];
        }

        /// <summary>
        /// Gets the Vector2 position of the current path node
        /// </summary>
        /// <returns>The Vector2 of the node</returns>
        public Vector2 GetCurrentPositionOnPath(int index)
        {
            return Nodes[index];
        }

        /// <summary>
        /// Gets the Vector2 postion of the next node on the path
        /// </summary>
        /// <returns></returns>
        public Vector2 GetNextPositionOnPath()
        {
            return Nodes[CurrentNodeIndex + 1];
        }

        /// <summary>
        /// Increases the Node index
        /// </summary>
        public void Next()
        {
            CurrentNodeIndex++;
        }

        /// <summary>
        /// Gets the next node index, will return 0 if the next index would be greater than the length of the path
        /// </summary>
        /// <param name="index">Current index</param>
        /// <returns>The index of the next node on the path</returns>
        public int Next(int index)
        {
            if (IsEndOfPath(index))
            {
                return 0;
            }
            return index++;
        }

        /// <summary>
        /// Decreases the Node index
        /// </summary>
        public void Previous()
        {
            CurrentNodeIndex--;
            if (CurrentNodeIndex < 0)
            {
                CurrentNodeIndex = Nodes.Count;
            }
        }

        /// <summary>
        /// Checks if its at the end of the path
        /// </summary>
        /// <returns>True if at the end of the path</returns>
        public bool IsEndOfPath()
        {
            return CurrentNodeIndex >= Nodes.Count;
        }

        /// <summary>
        /// Checks if its at the end of the path
        /// </summary>
        /// <returns>True if at the end of the path</returns>
        public bool IsEndOfPath(int index)
        {
            return index >= Nodes.Count;
        }

        /// <summary>
        /// Resets the node index to 0 (the start of the path)
        /// </summary>
        public void ResetNodeIndex()
        {
            CurrentNodeIndex = 0;
        }

    }
}
